//
//  MineViewController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/7/27.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "MineViewController.h"
#import "MyOneCell.h"
#import "MyTwoCell.h"

#import "MyCollectionVController.h"//我的收藏
#import "ChongZhiVController.h"//充值
#import "TiXianViewController.h"//提现

#import "MyBankCardController.h"
#import "MineMyAddressController.h"
#import "MyMessageViewController.h"
#import "MyConnectPeopleController.h"
#import "SettingVController.h"
#import "PersonInfoVController.h"
#import "RenZhengMarketController.h"
#import "MyMoneyVController.h"
#import "MyMoneyVController.h"

#import "BankCardModel.h"


@interface MineViewController ()<UITableViewDelegate ,UITableViewDataSource>
{
    NSString *nameStr;//昵称
    NSString *iamgeStr;//头像
    NSString *phoneStr;//电话
    NSString *blanceStr;//余额
    NSString *stateStr;//认证状态

    NSString *is_notice;//是否开启通知
    NSString *isSetPayPassword;//是否设置啦支付密码
    
    
}
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong)NSArray *titleArray;
@property (nonatomic, strong)UIButton *rightBtn2;//右侧按钮

@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation MineViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;
    [self getPersonInfoData];//获取个人信息
}
#pragma mark 获取个人信息数据
- (void)getPersonInfoData {
    
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    [HttpRequest getWithURLString:POST(Shop_PersonalGetPersonal) parameters:parameDic success:^(id responseObject) {
        
        if ([responseObject[@"ret"] intValue] != 200) {
            [MBProgressHUD showText:@"获取个人资料出错"];
            return ;
        }
        NSDictionary *tempD = responseObject[@"data"];
        
        if (![[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"0"]) {
            [MBProgressHUD showText:tempD[@"msg"]];
            return;
        }
        
        nameStr = tempD[@"info"][@"nickname"];
        phoneStr = tempD[@"info"][@"mobile"];
        iamgeStr = tempD[@"info"][@"headimgurl"];
        blanceStr = [NSString stringWithFormat:@"%@",tempD[@"info"][@"balance"]];
        stateStr = tempD[@"info"][@"shop_status_name"];
        
        is_notice = tempD[@"info"][@"is_notice"];

        //保存是否设置支付密码
        isSetPayPassword  = tempD[@"info"][@"isSetPayPassword"];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        //保存值(key值同名的时候会覆盖的)
        [defaults setObject:isSetPayPassword forKey:@"isPayPassWord"];
        //立即保存
        [defaults synchronize];
        

        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 1;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KShouYeBg;
    [self configureNaBar];
    [self configureViewController];
    _dataArray = [NSMutableArray array];
    _titleArray = @[@[@"我的收藏",@"地址管理",@"消息中心"],@[@"超市认证"],@[@"我的客服",@"设置"]];
    
    
    
}


- (void)configureNaBar {
    //设置导航栏按钮的边距
    _rightBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn2.frame= CGRectMake(0, 0, 60, 30);
    [_rightBtn2 setImage:[UIImage imageNamed:@"shouye_xiaoxi"] forState:(UIControlStateNormal)];
    [_rightBtn2 addTarget:self action:@selector(onClickRight) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn2];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
    
}

#pragma mark 消息事件
- (void)onClickRight {
    [self.view showTextNoActivity:@"我是消息" timeLength:(1)];
}
- (void)configureViewController {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, kScreen_Width, kScreen_Height+20) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = KShouYeBg;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = kSpNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MyOneCell" bundle:nil] forCellReuseIdentifier:@"MyOneCell"];
     [self.tableView registerNib:[UINib nibWithNibName:@"MyTwoCell" bundle:nil] forCellReuseIdentifier:@"MyTwoCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 3;
    }else if (section == 3){
        return 2;
    }else {
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.section == 0) {
        return 290;
    }else {
        return 60;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else {
    return 10;    
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 10)];
    bgV.backgroundColor = KShouYeBg;
    return bgV;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MyOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyOneCell"];
        cell.selectionStyle = kSeNone;
        
        cell.photoImageV.userInteractionEnabled = YES;
        if (nameStr.length == 0) {
            nameStr = @"暂没有设置哦";
        }
        cell.nameLB.text = nameStr;
        cell.phoneLB.text = phoneStr;
        
        cell.moneyLB.text = [NSString stringWithFormat:@"¥%@",blanceStr];
        [cell.photoImageV sd_setImageWithURL:[NSURL URLWithString:iamgeStr] placeholderImage:[UIImage imageNamed:@"quetu"]];
        UITapGestureRecognizer *tapPhoto = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapPhoto:)];
        [cell.photoImageV addGestureRecognizer:tapPhoto];
        
        
        cell.chongZhiView.userInteractionEnabled = YES;
        cell.tiXainView.userInteractionEnabled = YES;
        cell.yinHangCardView.userInteractionEnabled = YES;
        cell.chongZhiView.tag = 601;
        cell.tiXainView.tag = 602;
        cell.yinHangCardView.tag = 603;
        
        UITapGestureRecognizer *tapV = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOneCellView:)];
        
        UITapGestureRecognizer *tapV2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOneCellView:)];
        
        UITapGestureRecognizer *tapV3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOneCellView:)];
        
        
        [cell.chongZhiView addGestureRecognizer:tapV];
        [cell.tiXainView addGestureRecognizer:tapV2];
        [cell.yinHangCardView addGestureRecognizer:tapV3];
    
        
        [cell.myMoneyBtn addTarget:self action:@selector(handleMyMoneyAction:) forControlEvents:(UIControlEventTouchUpInside)];

        cell.messageBtn.hidden = YES;
        return cell;
    }
    else {
        MyTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTwoCell"];
        cell.selectionStyle = kSeNone;
        if (indexPath.section == 1) {
            cell.titLB.text = _titleArray[0][indexPath.row];
        }
        if (indexPath.section == 2) {
            cell.titLB.text = _titleArray[1][indexPath.row];
            cell.renZhengLB.hidden = NO;
        }
        if (indexPath.section == 3) {
            cell.titLB.text = _titleArray[2][indexPath.row];
        }
        
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            MyCollectionVController *myc = [[MyCollectionVController alloc] init];
            myc.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:myc animated:YES];
        }
        if (indexPath.row == 1) {
            MineMyAddressController *myc = [[MineMyAddressController alloc] init];
            myc.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:myc animated:YES];
        }
        
        if (indexPath.row == 2) {
            MyMessageViewController *address = [[MyMessageViewController alloc] init];
            UINavigationController *naV =[[UINavigationController alloc] initWithRootViewController:address];
            address.hidesBottomBarWhenPushed = YES;
            [self presentViewController:naV animated:YES completion:nil];
        }
        
    }
    
    if (indexPath.section == 2) {
        RenZhengMarketController  *address = [[RenZhengMarketController alloc] init];
        address.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:address animated:YES];
    }
    
    if (indexPath.section == 3) {
        if (indexPath.row == 0) {
           MyConnectPeopleController  *address = [[MyConnectPeopleController alloc] init];
            UINavigationController *naV =[[UINavigationController alloc] initWithRootViewController:address];
            address.hidesBottomBarWhenPushed = YES;
            [self presentViewController:naV animated:YES completion:nil];
        }
        if (indexPath.row == 1) {
            SettingVController  *address = [[SettingVController alloc] init];
            address.isNotice = is_notice;
            address.hidesBottomBarWhenPushed = YES;

            [self.navigationController pushViewController:address animated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma  mark 充值，提现，银行卡的视图轻拍事件
- (void)handleTapOneCellView:(UITapGestureRecognizer *)tap {
    
    if (tap.view.tag == 601) {
        ChongZhiVController *chong = [[ChongZhiVController alloc] init];
        chong.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:chong animated:YES];
    }
    if (tap.view.tag == 602) {
        
        //获取银行卡列表
     
            NSDictionary *saveDic = [SaveDataMethord readLoginData];
            NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
            [HttpRequest getWithURLString:POST(Shop_PersonalGetBankCardList) parameters:parameDic success:^(id responseObject) {
                if ([responseObject[@"ret"] intValue] != 200) {
                    [MBProgressHUD showText:@"获取银行卡出错"];
                    return ;
                }
                if (![[NSString stringWithFormat:@"%@",responseObject[@"data"][@"code"]] isEqualToString:@"0"]) {
                    [MBProgressHUD showText:responseObject[@"data"][@"msg"]];
                    return;
                }
                NSArray *cardList = [[responseObject objectForKey:@"data"]objectForKey:@"list"];
                self.dataArray =[BankCardModel mj_objectArrayWithKeyValuesArray:cardList];
                
                //判断如果没有银行卡的话，跳转到添加银行卡界面，如果有的话，直接跳转到提现界面
                if (self.dataArray.count == 0) {
                    [MBProgressHUD showText:@"请先添加银行卡，在进行提现"];
                    
                    return;
                }
                
                
                TiXianViewController *chong = [[TiXianViewController alloc] init];
                chong.hidesBottomBarWhenPushed = YES;
                chong.bankModel = self.dataArray[0];
                chong.typePerson = @"chaoshi";
                [self.navigationController pushViewController:chong animated:YES];
                
                
            } failure:^(NSError *error) {
                
            }];
  
        

        
     
        
        
    }
    if (tap.view.tag == 603) {
        MyBankCardController *chong = [[MyBankCardController alloc] init];
        chong.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:chong animated:YES];
    }
}



//进入个人资料
- (void)handleTapPhoto:(UITapGestureRecognizer *)tap {
    PersonInfoVController *chong = [[PersonInfoVController alloc] init];
    chong.hidesBottomBarWhenPushed = YES;
    chong.pohotUrlStr =iamgeStr;
    chong.nameStr = nameStr;
    [self.navigationController pushViewController:chong animated:YES];
}

//进入我的钱包
- (void)handleMyMoneyAction:(UIButton *)btn {
    MyMoneyVController *my = [[MyMoneyVController alloc] init];
    my.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:my animated:YES];
}

@end
